package action;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import utils.SHA1;

public class WechatCallbackApi extends BaseAction {

    // token
    public static String TOKEN = "845C2550903CE6FA54CACDB82EAD4350";

    public String execute() {
        // 签名
        String signature = request.getParameter("signature");
        // 
        String echostr = request.getParameter("echostr");
        // 时间戳
        String timestamp = request.getParameter("timestamp");
        // 随机数
        String nonce = request.getParameter("nonce");

        String[] str = { TOKEN, timestamp, nonce };
        Arrays.sort(str); // 排序
        String bigStr = str[0] + str[1] + str[2];
        
        // SHA1加密
        String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase();

        // 判断返回值
        String outputStr = digest.equals(signature) ? echostr : "";
        try {
            inputStream = new ByteArrayInputStream(outputStr.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        
        return SUCCESS;

    }
    
}
